﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using WCSY.BLL;

public partial class SystemMangement_ApprovFlow_DocumentControlApp : System.Web.UI.Page
{
    fg_operatesAppManager oappm = new fg_operatesAppManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack){
            //fg_operatesAppManager oappm = new fg_operatesAppManager();
            DataTable dt = oappm.GetAllList().Tables[0];
            gvBind(dt);

        }
    }

    //绑定数据
    public void gvBind(DataTable dt) {

        this.gvOperatesApp.DataSource = dt;
        this.gvOperatesApp.DataBind();
    
    }
    //提交功能
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<string> sqlList = new List<string>();

        foreach (GridViewRow row in this.gvOperatesApp.Rows)
        {
            CheckBox cb = row.FindControl("chkItem") as CheckBox;
            string opNo = row.Cells[2].Text.ToString();
            CheckBox isDefAppCb = row.FindControl("chkisDefaultApp") as CheckBox;
            if(cb.Checked==false && isDefAppCb.Checked==true){
                tool.Alert("启用默认审批必须启用审批流！",this);
                return;
            }

            if (cb.Checked == true && isDefAppCb.Checked == true)
            {
                
                //设置启用
                sqlList.Add(oappm.Update(opNo, 1,1));


            }
            else if (cb.Checked == true && isDefAppCb.Checked == false)
            {

                //设置启用,但不启用默认审批
                sqlList.Add(oappm.Update(opNo, 1, 0));
            }
            else { 
                //设置不启用
                sqlList.Add(oappm.Update(opNo, 0, 0));
            }

        }

        try
        {

            string suc = tool.executeSqlTran(sqlList);
            if (!suc.Equals("suc"))
            {
                throw new Exception(suc);
            }
        }
        catch (Exception ex)
        {
           
            tool.Alert(ex.Message+" 提交失败",this);
            return;
        }

        //this.RegisterStartupScript("alert", "<script>alert('提交成功！')</script>");
        tool.Alert("提交成功！",this);
        //重新绑定
        DataTable dt = oappm.GetAllList().Tables[0];
        gvBind(dt);
    }
    //绑定时，设置启用复选框是否勾选
    protected void gvOperatesApp_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
        //隐藏是否启用列
        if(e.Row.RowType== DataControlRowType.DataRow || e.Row.RowType== DataControlRowType.Header){
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            string isApp = e.Row.Cells[4].Text;
            string isDefaultApp = e.Row.Cells[5].Text;
            CheckBox chkItemBox = e.Row.FindControl("chkItem") as CheckBox;
            CheckBox chkIsDefaultApp = e.Row.FindControl("chkisDefaultApp") as CheckBox;
            if (isApp.Equals("1"))
            {
                chkItemBox.Checked = true;
            }
            if(isDefaultApp.Equals("1")){
                chkIsDefaultApp.Checked = true;
            }

        }
    }
}
